home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1999 May / Cd Pc Users extra 20 mayo 1999.iso / Prog / Inst / FTP / MULTI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-18  |  4.5 KB  |  171 lines

  1. /*
  2. **  MULTI.C 
  3. **
  4. **  This example demonstrates the use of threads. One thread is started 
  5. **  for each FTP account. The thread logs onto the FTP server and retrieves
  6. **  a list of files, then logs off.
  7. **
  8. **  Edit the FTP account information on line below (line 123) before compiling.
  9. **
  10. **  Must compile with -MT flag (Microsoft).
  11. **
  12. */
  13.  
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <process.h>
  19. #include "fce.h"
  20.  
  21. #define NBR_THREADS   10
  22.  
  23. #define QUOTE 0x22
  24.  
  25. typedef struct
  26. {int  IsRunning;
  27.  int  ThreadID;
  28.  char Srvr[40];
  29.  char User[40];
  30.  char Pass[40];
  31.  char Temp[1024];
  32.  long WorkCount;
  33.  long WaitCount;
  34. } FtpParamType;
  35.  
  36. static FtpParamType FtpParams[NBR_THREADS]; 
  37.  
  38. /* FTP thread function */
  39.  
  40. void FtpThread(PVOID DataPtr)
  41. {int Code;
  42.  LPSTR Ptr1;
  43.  LPSTR Ptr2;
  44.  int ThreadID;
  45.  FtpParamType *ParamPtr;
  46.  char Temp[40];
  47.  ParamPtr = (FtpParamType *) DataPtr;
  48.  ParamPtr->IsRunning = TRUE;
  49.  /* starting Ftp thread to check email status */
  50.  ThreadID = ParamPtr->ThreadID;
  51.  printf("Thread %d: Started.\n",ThreadID);
  52.  /* define diagnostics log file */
  53.  sprintf(Temp,"MULTI%1d.LOG",ParamPtr->ThreadID); 
  54.  fceSetString(ThreadID,FCE_SET_LOG_FILE, (LPSTR)Temp); 
  55.  /* connect to FTP server */
  56.  printf("Thread %d: Connecting to Server %s for User %s\n", 
  57.     ThreadID, (LPSTR)&(ParamPtr->Srvr),(LPSTR)&(ParamPtr->User) );
  58.  Code = fceConnect(ThreadID,
  59.     (LPSTR)&(ParamPtr->Srvr),      /* FTP Server */
  60.     (LPSTR)&(ParamPtr->User),      /* User */ 
  61.     (LPSTR)&(ParamPtr->Pass));     /* Password */
  62.  if(Code<0)
  63.     {printf("Thread %d: Cannot connect to %s\n", 
  64.        ThreadID,(LPSTR)&(ParamPtr->Srvr));
  65.      ParamPtr->IsRunning = FALSE;
  66.      return;  
  67.     }  
  68.  Sleep(0);
  69.  /* get list of filenames */
  70.  Code = fceGetList(ThreadID,FCE_NAME_LIST,(LPSTR)ParamPtr->Temp,1024); 
  71.  if(Code<0)
  72.    {printf("Thread %d: Error \n",ThreadID);
  73.     ParamPtr->IsRunning = FALSE;
  74.     fceClose(ThreadID);
  75.     return;
  76.    }
  77.  printf("Thread %d: File list follows\r\n",ThreadID); 
  78.  Ptr1 = (LPSTR)ParamPtr->Temp;
  79.  while(1)
  80.    {Ptr2 = strchr(Ptr1,'\r');
  81.     if(Ptr2==NULL) break;
  82.     *Ptr2 = '\0';
  83.     printf("Thread %d: %s\n", ThreadID,Ptr1);
  84.     Sleep(0);
  85.     Ptr1 = Ptr2 + 2;
  86.    }
  87.  fceClose(ThreadID);
  88.  Sleep(0);
  89.  /* thread has completed */
  90.  ParamPtr->IsRunning = FALSE;
  91.  printf("Thread %d: Completed.\n", ThreadID);
  92. }
  93.  
  94. void AddUser(int Index, LPSTR Srvr, LPSTR User, LPSTR Pass)
  95. {strcpy((LPSTR)(&FtpParams[Index].Srvr), Srvr);
  96.  strcpy((LPSTR)(&FtpParams[Index].User), User); 
  97.  strcpy((LPSTR)(&FtpParams[Index].Pass), Pass);
  98.  FtpParams[Index].IsRunning = FALSE;
  99.  FtpParams[Index].ThreadID  = Index;
  100.  FtpParams[Index].WorkCount = 0;
  101.  FtpParams[Index].WaitCount = 0;
  102. }
  103.  
  104. /*** MAIN ***/
  105.  
  106. void main(int argc, char *argv[])
  107. {int i;
  108.  int Version;
  109.  int Running;
  110.  unsigned long TimeStart;
  111.  unsigned long TimeStop;
  112.  int ThreadsToRun;
  113.  
  114.  if(argc!=1)
  115.    {printf("Usage: MULTI\n");
  116.     exit(1);
  117.    } 
  118.    
  119.  /* hard code one FTP setting per thread   */
  120.  /* 'ThreadsToRun' must be <= 'NBR_THEADS' */
  121.  
  122.  AddUser(0, "10.0.0.1", "mike",    "mike");
  123.  AddUser(1, "10.0.0.1", "pam",     "pam");
  124.  
  125.  AddUser(2, "10.0.0.2", "mike",    "mike");
  126.  AddUser(3, "10.0.0.2", "pam",     "pam");
  127.  //AddUser(4, "10.0.0.2", "lauren",  "lauren");
  128.  //AddUser(5, "10.0.0.2", "bingo",   "bingo");
  129.  
  130.  ThreadsToRun = 4;  // Must be <= NBR_THREADS
  131.  
  132.  /* allocate channels (one per thread) */
  133.  fceAttach(ThreadsToRun);
  134.  
  135.  /* display FCE parameters */
  136.  Version = fceGetInteger(0,FCE_GET_VERSION);
  137.  printf("FCE4C Version %1x.%1x.%1x\n",0x0f&(Version>>8),0x0f&(Version>>4),0x0f&Version); 
  138.  
  139.  /* set internal SEE SleepTime value (default = 20) */
  140.  fceSetInteger(0, FCE_SET_SLEEP_TIME, 100);
  141.  
  142.  /* start FTP threads */
  143.  for(i=0;i<ThreadsToRun;i++) 
  144.   {
  145.    _beginthread(FtpThread, 0, (void *)&FtpParams[i] );
  146.   }
  147.  Sleep(100);
  148.  /* get start start time */
  149.  TimeStart = GetCurrentTime();
  150.  
  151.  /* run until all threads are done */
  152.  while(1)
  153.    {/* are all threads done ? */
  154.     Running = 0;
  155.     for(i=0;i<ThreadsToRun;i++) if(FtpParams[i].IsRunning) Running++;
  156.     if(Running==0)
  157.       {TimeStop = GetCurrentTime();
  158.        printf("All threads have terminated.\n");
  159.        for(i=0;i<ThreadsToRun;i++)
  160.          printf("Thread %d: WorkCount = %d; WaitCount = %d\n", 
  161.            i, FtpParams[i].WorkCount, FtpParams[i].WaitCount);
  162.        printf("Total time = %d ms per user\n", (TimeStop - TimeStart) / ThreadsToRun);
  163.        fceRelease();
  164.        return;
  165.       }
  166.     Sleep(0);
  167.    }
  168.  
  169. } /* end main */
  170.  
  171.